package sibu.cn.mydemo.greendao;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import java.util.List;

import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;
import sibu.cn.mydemo.DaoMaster;
import sibu.cn.mydemo.DaoSession;
import sibu.cn.mydemo.Note;
import sibu.cn.mydemo.NoteDao;
import sibu.cn.mydemo.R;

public class GreenDaoActivity extends AppCompatActivity {

    private DaoMaster.DevOpenHelper devOpenHelper;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;

    private EditText editText;
    private ListView listView;
    private SimpleCursorAdapter simpleCursorAdapter;
    private Cursor cursor;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_green_dao);
        initView();
    }
    public void initView(){

        editText = (EditText) findViewById(R.id.greendao_my_content_edt);
        listView = (ListView) findViewById(R.id.listview_green_dao);


        //第一个参数为Context，第二个参数为数据表的文件名，这个我们可以以"表名+s+'-db'"来命名，第三个通常为null
        devOpenHelper = new DaoMaster.DevOpenHelper(this, "students-db", null);
        db = devOpenHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        //得到StudentDAO对象，所以在这看来，对于这三个DAO文件，我们更能接触到的是StudentDao文件，进行CRUD操作也是通过StudentDao对象来操作
        noteDao = daoSession.getNoteDao();

        //遍历表中的所有数据
        cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, null);
        //通过StudentDao的静态内部类得到Name字段对应表中的列名
        String[] from = {NoteDao.Properties.Date.columnName, NoteDao.Properties.Id.columnName};
        int[] to = {android.R.id.text1, android.R.id.text2};
        simpleCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to, SimpleCursorAdapter.NO_SELECTION);
        listView.setAdapter(simpleCursorAdapter);
    }

    public void click(View view){
        switch (view.getId()){
            case R.id.add_green_dao:
                add();
                Cursor cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, null);
                simpleCursorAdapter.swapCursor(cursor);//交换cursor数据集从而实时更新UI
                //cursor.requery();//这个也可以实时更新UI，不过官方已经不推荐使用了，因为它可能会阻塞UI线程造成ANR
                break;
            case R.id.delete_green_dao:
                delete();
                Cursor cursor2 = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, null);
                simpleCursorAdapter.swapCursor(cursor2);
                break;
            case R.id.update_green_dao:
                update();
                Cursor cursor3 = db.query(noteDao.getTablename(), noteDao.getAllColumns(), null, null, null, null, null);
                simpleCursorAdapter.swapCursor(cursor3);
                break;
            case R.id.query_green_dao:
                search();
                break;

        }
    }
    private void add() {
        String content = editText.getText().toString().trim();
//        DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.CHINA);
        Note note = new Note(null, content,"text", null);//由于主键id之前设置了自增长，所以传入null即可
        noteDao.insert(note);//插入数据
    }

    private void delete() {
        String content = editText.getText().toString().trim();
        noteDao.deleteByKey(Long.valueOf(content));//通过主键来删除某行数据
//        dao.deleteAll();//全部删除
    }

    private void update() {
        String content = editText.getText().toString().trim();
        //update只能通过主键来更新某个实体对应的数据，这里的主键为id
        noteDao.update(new Note(Long.valueOf(content), "这是修改的","update", null));
    }

    private void search() {
        String content = editText.getText().toString().trim();
        //通过queryBuilder()来查询可以更方便的设置查询的条件,其中eq()方法表示equal()，判断Name是否和content相等，然后通过build建立查询
        Query<Note> query = noteDao.queryBuilder().where(NoteDao.Properties.Id.eq(content)).build();
        List<Note> list = query.list();
        //下面两个Flag的设置可以在控制台中打印出此次查询的sql语句和value值
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
        new AlertDialog.Builder(this).setMessage(list.get(0).getId()+","+list.get(0).getText()).setPositiveButton("确定",null).create().show();
    }

    public static void jumpGreenDaoActivity(Context context){

        Intent intent = new Intent(context,GreenDaoActivity.class);
        context.startActivity(intent);
    }
}
